const express = require('express')
const router = express.Router()
const models = require('../../models')
const { success, error } = require('../../utils/message')
const checkPermission = require('../../middlewares/check-permission');
const Op = models.Sequelize.Op

// 查询快递列表
router.get('/', checkPermission(), async function (req, res, next) {
  const name = req.query.name
  const where = {}
  const currentPage = parseInt(req.query.currentPage) || 1
  const pageSize = parseInt(req.query.pageSize) || 10

  // 模糊查询
  if (name) {
    where.name = {
      [Op.like]: '%' + name + '%'
    }
  }

  const result = await models.Express.findAndCountAll({
    order: [['sort', 'ASC']],
    where: where,
    offset: (currentPage - 1) * pageSize,
    limit: pageSize
  })

  const data = {
    expresses: result.rows,
    pagination: {
      currentPage: currentPage,
      pageSize: pageSize,
      total: result.count
    }
  }
  success(res, '查询成功', data)
})

// 新增
router.post('/', checkPermission(), async function (req, res, next) {
  const name = req.body.name
  const code = req.body.code
  const sort = req.body.sort

  if (!name) {
    return error(res, '请填写快递名称')
  }

  if (!code) {
    return error(res, '请填写快递公司代码')
  }

  if (!sort) {
    return error(res, '请填写排序')
  }

  if (!Number(sort)) {
    return error(res, '排序必须是数字')
  }

  let express = await models.Express.findOne({ where: { name: name } })
  if (express) {
    return error(res, '当前快递已经存在，请换一个快递名称')
  }

  express = await models.Express.create(req.body)
  success(res, '新增成功', { express })
})

// 查询当前记录
router.get('/:id', checkPermission(),  async function (req, res, next) {
  const id = req.params.id

  const express = await models.Express.findByPk(id)
  if (!express) {
    return error(res, '当前快递不存在')
  }

  success(res, '查询成功', { express })
})

// 修改
router.put('/:id', checkPermission(),  async function (req, res, next) {
  const id = req.params.id
  const name = req.body.name
  const code = req.body.code
  const sort = req.body.sort

  if (!name) {
    return error(res, '请填写快递名称')
  }

  if (!code) {
    return error(res, '请填写快递公司代码')
  }

  if (!sort) {
    return error(res, '请填写排序')
  }

  if (!Number(sort)) {
    return error(res, '排序必须是数字')
  }

  const express = await models.Express.findByPk(id)
  if (!express) {
    return error(res, '当前快递不存在，无法修改')
  }

  express.update(req.body)
  success(res, '修改成功', { express })
})

// 删除
router.delete('/:id', checkPermission(),  async function (req, res, next) {
  const id = req.params.id

  const express = await models.Express.findByPk(id)
  if (!express) {
    return error(res, '当前快递不存在，无法删除')
  }

  express.destroy()
  success(res, '删除成功')
})


// 修改排序
router.patch('/:id/change_sort', checkPermission(),  async function (req, res, next) {
  const id = req.params.id
  const sort = req.body.sort

  const express = await models.Express.findByPk(id)
  if (!express) {
    return error(res, '当前快递不存在，无法修改排序')
  }

  if (!sort) {
    return error(res, '请填写排序')
  }

  if (!Number(sort)) {
    return error(res, '排序必须是数字')
  }

  express.update({ sort: sort })
  success(res, '修改排序成功 ')
})

module.exports = router
